home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>10.3. Lists, Lists And More Lists</title>
- <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
- <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
- <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
- <link rel="start" href="index.html" title="GIMP User Manual" />
- <link rel="up" href="ch02s10.html" title="10. A Script-Fu Tutorial" />
- <link rel="prev" href="ch02s10s02.html" title="10.2. Variables And Functions" />
- <link rel="next" href="ch02s10s04.html" title="10.4. Your First Script-Fu Script" />
- </head>
- <body>
- <div xmlns="" class="navheader">
- <table width="100%" summary="Navigation header">
- <tr>
- <th colspan="3" align="center" id="chaptername">10. A Script-Fu Tutorial</th>
- </tr>
- <tr>
- <td width="20%" align="left"><a accesskey="p" href="ch02s10s02.html">Prev</a> </td>
- <th width="60%" align="center" id="sectionname">10.3. Lists, Lists And More Lists</th>
- <td width="20%" align="right"> <a accesskey="n" href="ch02s10s04.html">Next</a></td>
- </tr>
- </table>
- <hr />
- </div>
- <div class="sect2" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h3 class="title"><a id="id3429666"></a>10.3. Lists, Lists And More Lists</h3>
- </div>
- </div>
- </div>
- <p>
- We've trained you in variables and functions, and now enter the
- murky swamps of Scheme's lists.
- </p>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3429679"></a>Defining A List</h4>
- </div>
- </div>
- </div>
- <p>
- Before we talk more about lists, it is necessary that you know
- the difference between atomic values and lists.
- </p>
- <p>
- You've already seen atomic values when we initialized
- variables in the previous lesson. An atomic value is a single
- value. So, for example, we can assign the variable "x" the
- single value of 8 in the following statement:
- </p>
- <pre class="programlisting">
- (let* ( (x 8) ) x)
- </pre>
- <p>
- (We added the expression <tt class="varname">x</tt> at the end to print out the value
- assigned to <tt class="varname">x</tt>-- normally you won't need to do this. Notice how
- <tt class="code">let*</tt> operates just like a function: The value of
- the last statement is the value returned.)
- </p>
- <p>
- A variable may also refer to a list of values, rather than a
- single value. To assign the variable <tt class="varname">x</tt> the
- list of values 1, 3, 5, we'd type:
- </p>
- <pre class="programlisting">
- (let* ( (x '(1 3 5))) x)
- </pre>
- <p>
- Try typing both statements into the Script-Fu Console and
- notice how it replies. When you type the first statement in,
- it simply replies with the result:
- </p>
- <pre class="programlisting">
- 8
- </pre>
- <p>
- However, when you type in the other statement, it replies with
- the following result:
- </p>
- <pre class="programlisting">
- (1 3 5)
- </pre>
- <p>
- When it replies with the value 8 it is informing you that
- <tt class="varname">x</tt> contains the atomic value 8. However,
- when it replies with (1 3 5), it is then informing you that
- <tt class="varname">x</tt> contains not a single value, but a list
- of values. Notice that there are no commas in our declaration
- or assignment of the list, nor in the printed result.
- </p>
- <p>
- The syntax to define a list is:
- </p>
- <pre class="programlisting">
- '(a b c)
- </pre>
- <p>
- where <tt class="varname">a</tt>, <tt class="varname">b</tt>, and
- <tt class="varname">c</tt> are literals. We use the apostrophe (')
- to indicate that what follows in the parentheses is a list of
- literal values, rather than a function or expression.
- </p>
- <p>
- An empty list can be defined as such:
- </p>
- <pre class="programlisting">
- '()
- </pre>
- <p>
- or simply:
- </p>
- <pre class="programlisting">
- ()
- </pre>
- <p>
- Lists can contain atomic values, as well as other lists:
- </p>
- <pre class="programlisting">
- (let*
- (
- (x
- '("The Gimp" (1 2 3) ("is" ("great" () ) ) )
- )
- )
-
- x
- )
- </pre>
- <p>
- Notice that after the first apostrophe, you no longer need to
- use an apostrophe when defining the inner lists. Go ahead and
- copy the statement into the Script-Fu Console and see what it
- returns.
- </p>
- <p>
- You should notice that the result returned is not a list of
- single, atomic values; rather, it is a list of a literal <tt class="code">("The
- Gimp")</tt>, the list <tt class="code">(1 2 3)</tt>, etc.
- </p>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3429854"></a>How To Think Of Lists</h4>
- </div>
- </div>
- </div>
- <p>
- It's useful to think of lists as composed of a "head" and a
- "tail." The head is the first element of the list, the tail
- the rest of the list. You'll see why this is important when we
- discuss how to add to lists and how to access elements in the
- list.
- </p>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3429871"></a>Creating Lists Through Concatenation (The Cons Function)</h4>
- </div>
- </div>
- </div>
- <p>
- One of the more common functions you'll encounter is the cons
- function. It takes a value and prepends it to its second
- argument, a list. From the previous section, I suggested that
- you think of a list as being composed of an element (the head)
- and the remainder of the list (the tail). This is exactly how
- cons functions -- it adds an element to the head of a
- list. Thus, you could create a list as follows:
- </p>
- <pre class="programlisting">
- (cons 1 '(2 3 4) )
- </pre>
- <p>
- The result is the list <tt class="code">(1 2 3 4)</tt>.
- </p>
- <p>
- You could also create a list with one element:
- </p>
- <pre class="programlisting">
- (cons 1 () )
- </pre>
- <p>
- You can use previously declared variables in place of any
- literals, as you would expect.
- </p>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3429918"></a>Defining A List Using The list Function</h4>
- </div>
- </div>
- </div>
- <p>
- To define a list composed of literals or previously declared
- variables, use the list function:
- </p>
- <pre class="programlisting">
- (list 5 4 3 a b c)
- </pre>
- <p>
- This will compose and return a list containing the values held
- by the variables <tt class="varname">a</tt>, <tt class="varname">b</tt>
- and <tt class="varname">c</tt>. For example:
- </p>
- <pre class="programlisting">
- (let* (
- (a 1)
- (b 2)
- (c 3)
- )
- (list 5 4 3 a b c)
- )
- </pre>
- <p>
- This code creates the list <tt class="code">(5 4 3 1 2 3)</tt>.
- </p>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3429970"></a>Accessing Values In A List</h4>
- </div>
- </div>
- </div>
- <p>
- To access the values in a list, use the functions <tt class="code">car</tt> and <tt class="code">cdr</tt>,
- which return the first element of the list and the rest of the
- list, respectively. These functions break the list down into
- the head::tail construct I mentioned earlier.
- </p>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3429994"></a>The <tt class="code">car</tt> Function</h4>
- </div>
- </div>
- </div>
- <p>
- <tt class="code">car</tt> returns the first element of the list (the
- head of the list). The list needs to be non-null. Thus, the
- following returns the first element of the list:
- </p>
- <pre class="programlisting">
- (car '("first" 2 "third"))
- </pre>
- <p>
- which is:
- </p>
- <pre class="programlisting">
- "first"
- </pre>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3430031"></a>The <tt class="code">cdr</tt> function</h4>
- </div>
- </div>
- </div>
- <p>
- <tt class="code">cdr</tt> returns the rest of the list after the first
- element (the tail of the list). If there is only one element
- in the list, it returns an empty list.
- </p>
- <pre class="programlisting">
- (cdr '("first" 2 "third"))
- </pre>
- <p>
- returns:
- </p>
- <pre class="programlisting">
- (2 "third")
- </pre>
- <p>
- whereas the following:
- </p>
- <pre class="programlisting">
- (cdr '("one and only"))
- </pre>
- <p>
- returns:
- </p>
- <pre class="programlisting">
- ()
- </pre>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3430086"></a>Accessing Other Elements In A List</h4>
- </div>
- </div>
- </div>
- <p>
- OK, great, we can get the first element in a list, as well as
- the rest of the list, but how do we access the second, third
- or other elements of a list? There exist several "convenience"
- functions to access, for example, the head of the head of the
- tail of a list (<tt class="code">caadr</tt>), the tail of the tail of a
- list (<tt class="code">cddr</tt>), etc.
- </p>
- <p>
- The basic naming convention is easy: The a's and d's represent
- the heads and tails of lists, so
- </p>
- <pre class="programlisting">
- (car (cdr (car x) ) )
- </pre>
- <p>
- could be written as:
- </p>
- <pre class="programlisting">
- (cadar x)
- </pre>
- <p>
- To view a full list of the list functions, refer to the
- Appendix, which lists the available functions for the version
- of Scheme used by Script-Fu.
- </p>
- <p>
- To get some practice with list-accessing functions, try typing
- in the following (except all on one line if you're using the
- console); use different variations of car and cdr to access
- the different elements of the list:
- </p>
- <pre class="programlisting">
- (let* (
- (x '( (1 2 (3 4 5) 6) 7 8 (9 10) )
- )
- )
- ; place your car/cdr code here
- )
- </pre>
- <p>
- Try accessing the number 3 in the list using only two function
- calls. If you can do that, you're on your way to becoming a
- Script-Fu Master!
- </p>
- <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
- <table border="0" summary="Note">
- <tr>
- <td rowspan="2" align="center" valign="top" width="25">
- <img alt="[Note]" src="../images/note.png" />
- </td>
- <th align="left">Note</th>
- </tr>
- <tr>
- <td colspan="2" align="left" valign="top">
- <p>
- In Scheme, a semicolon (";") marks a comment. It, and
- anything that follows it on the same line, are ignored by the
- script interpreter, so you can use this to add comments to jog
- your memory when you look at the script later.
- </p>
- </td>
- </tr>
- </table>
- </div>
- </div>
- </div>
- <div class="navfooter">
- <hr />
- <table width="100%" summary="Navigation footer">
- <tr>
- <td width="40%" align="left"><a accesskey="p" href="ch02s10s02.html">Prev</a> </td>
- <td width="20%" align="center">
- <a accesskey="u" href="ch02s10.html">Up</a>
- </td>
- <td width="40%" align="right"> <a accesskey="n" href="ch02s10s04.html">Next</a></td>
- </tr>
- <tr>
- <td width="40%" align="left" valign="top">10.2. Variables And Functions </td>
- <td width="20%" align="center">
- <a accesskey="h" href="index.html">Home</a>
- </td>
- <td width="40%" align="right" valign="top"> 10.4. Your First Script-Fu Script</td>
- </tr>
- </table>
- </div>
- </body>
- </html>
-